﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.ComponentModel;
using Tools;

using System.Windows.Threading;

namespace Pujcovna.Loading
{
    public partial class Loading : Window
    {
        private readonly BackgroundWorker worker = new BackgroundWorker();

        Thread loadingThread;
        Storyboard Showboard;
        Storyboard Hideboard;
        private delegate void ShowDelegate(string txt);
        private delegate void HideDelegate();
        ShowDelegate showDelegate;
        HideDelegate hideDelegate;

        public Loading()
        {
            InitializeComponent();
            showDelegate = new ShowDelegate(this.showText);
            hideDelegate = new HideDelegate(this.hideText);
            Showboard = this.Resources["showStatus"] as Storyboard;
            Hideboard = this.Resources["HideStatus"] as Storyboard;
        }

        private void Loading_Loaded_1(object sender, RoutedEventArgs e)
        {
            (FindResource("showMe") as Storyboard).Begin(this);
            loadingThread = new Thread(load);
            loadingThread.Start();
            
        }

        private void load()
        {
          
            //Thread.Sleep(500);
            this.Dispatcher.Invoke(showDelegate, "Čarují usměvy na tváři");
       //  Thread.Sleep(1000);
          // SettingsLib.Kurz.KurzControl.LoadKurz();
           initDatabases();
         this.Dispatcher.Invoke(hideDelegate);

          //  Thread.Sleep(500);
          /*  this.Dispatcher.Invoke(showDelegate, "Nabourávám se do Pentagonu");
           Thread.Sleep(1000);
            //load data
            this.Dispatcher.Invoke(hideDelegate);


            Thread.Sleep(1000);
            this.Dispatcher.Invoke(showDelegate, "Stahuji aktivační kódy");
            Thread.Sleep(2000);
            //load data
            this.Dispatcher.Invoke(hideDelegate);

            Thread.Sleep(1000);
            this.Dispatcher.Invoke(showDelegate, "Rakety odpáleny!"); 
           Thread.Sleep(2000);
            //load data 
            this.Dispatcher.Invoke(hideDelegate);
            
            //close the window
            //Thread.Sleep(5000);
          
            //init databáze
          
            */
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate() { Close(); });
            Testy();
        }

        private void Testy()
        {
          //  MicroStarPrinter.Printer.testPrint();
           // PrinterStarMicronic.Printer.testPrint();    //poze na testovani tiskarny
        }

        private void initDatabases()
        {
           
            DataLib.Settings.databases.dbZakaznici = new DB(DataLib.Properties.Settings.Default.ZakazniciConnectionString);
            DataLib.Settings.databases.dbSklad = new DB(DataLib.Properties.Settings.Default.SkladConnectionString);
            DataLib.Settings.databases.dbZakaznici = new DB(DataLib.Properties.Settings.Default.ZakazniciConnectionString);
            DataLib.Settings.databases.dbSklad = new DB(DataLib.Properties.Settings.Default.SkladConnectionString);
            // DataLib.Settings.databases.dbSettings = new DB(DataLib.Properties.Settings.Default.Settings_ConnectionString);
            DataLib.Settings.databases.dbCeny = new DB(DataLib.Properties.Settings.Default.CenyConnectionString);
            DataLib.Settings.databases.dbCasy = new DB(DataLib.Properties.Settings.Default.Casy_ConnectionString);
            DataLib.Settings.databases.dbOP = new DB(DataLib.Properties.Settings.Default.OPConnectionString);
            DataLib.Settings.databases.dbPujcky = new DB(DataLib.Properties.Settings.Default.PujckyConnectionString);
        }

        private void showText(string txt)
        {
            txtLoading.Text = txt;
            BeginStoryboard(Showboard);
        }
        private void hideText()
        {
            BeginStoryboard(Hideboard);
        }
    }
}
